package org.scohen.juploadr.ui;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Shell;
import org.scohen.juploadr.prefs.Configuration;

public class ProgressDialog extends Dialog {
    private static final int HORIZONTAL_INDENT = 5;
    private String message;
    private int min;
    private int max;
    public ProgressBar bar;
    Shell shell;
    private Shell parent;
    Label msgLabel;
    private Display display;
    private Task task;
    private Canvas icon;

    protected ProgressDialog(Shell parent, int style) {
        super(parent, style);
    }

    public ProgressDialog(Shell parent, Task t) {
        this(parent, 0);
        display = parent.getDisplay();
        this.parent = parent;
        this.task = t;
        this.task.setParent(this);

    }

    public void open() {
        shell = new Shell(parent, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
        shell.setText(task.getTitle());
        if (!Configuration.isMac()) {
            shell.setImage(ReusableUIFactory.getInstance().getTinyLogoImage());
        }
        GridLayout layout = new GridLayout();
        layout.marginBottom = 5;
        layout.marginTop = 5;
        layout.numColumns = 2;
        shell.setLayout(layout);
        getIcon();
        getMessageLabel();
        getProgressBar();

        shell.pack();
        shell.open();

        while (!shell.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
        }

    }

    public void close() {
        shell.setVisible(false);
        shell.close();
        disposed();
    }

    public void execute() {
        task.setParent(this);
        if (max > min) {
            task.start();
            open();
        }
    }

    /**
     * 
     */
    private void getProgressBar() {
        bar = new ProgressBar(shell, SWT.HORIZONTAL);
        GridData data = new GridData(SWT.FILL, SWT.END, true, false);
        data.horizontalIndent = HORIZONTAL_INDENT;
        bar.setLayoutData(data);
        bar.setMaximum(max);
        bar.setMinimum(min);
    }

    /**
     * 
     */
    private void getMessageLabel() {
        msgLabel = new Label(shell, SWT.HORIZONTAL);
        GridData data = new GridData(SWT.FILL, SWT.FILL, true, false);
        GC gc = new GC(msgLabel);
        data.widthHint = gc.textExtent("MMMMMMMMMMMMMMMMMMMMMMMMMM").x;
        data.heightHint = gc.textExtent("0\n0\n0\n0\n0").y;
        gc.dispose();
        data.horizontalIndent = HORIZONTAL_INDENT;
        msgLabel.setLayoutData(data);
        msgLabel.setText(message);
    }

    private void getIcon() {
        icon = new Canvas(shell, SWT.NONE);
        final Task t = task;
        icon.addPaintListener(new PaintListener() {
            public void paintControl(org.eclipse.swt.events.PaintEvent event) {
                GC gc = event.gc;
                Image icon = t.getIcon();
                ImageData iconData = icon.getImageData();
                gc.drawImage(icon, 0, 0, iconData.width, iconData.height, 0, 0, 64, 64);
            }
        });
        GridData data = new GridData();
        data.horizontalAlignment = SWT.FILL;
        data.verticalAlignment = SWT.FILL;
        data.verticalSpan = 2;
        icon.setLayoutData(data);
    }

    private void disposed() {
        if (!shell.isDisposed()) {
            bar.dispose();
            shell.dispose();
            msgLabel.dispose();
            icon.dispose();
        }
    }

    public int getMax() {
        return max;
    }

    public void setMax(int max) {
        this.max = max;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public int getMin() {
        return min;
    }

    public void setMin(int min) {
        this.min = min;
    }

}
